package com.qf.controller.rider;

import com.qf.entitys.RespBean;
import com.qf.entitys.Rider;
import com.qf.service.RiderService;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/rider")
public class RiderController {

    @Resource
    private RiderService service;

    @GetMapping("/login")
    public String loginindex(){
        return "rider/login";
    }
    @GetMapping("index")
    public String index(){
        return "rider/index";
    }
    @PostMapping("/login")
    @ResponseBody
    public RespBean login(String login_name,String login_pwd,HttpServletRequest request){
        Rider rider1 = service.selectRiderByLoginName(login_name);
        System.out.println(rider1);
        String s= DigestUtils.md5DigestAsHex(login_pwd.getBytes());
        if (rider1==null){
            return RespBean.error("用户名不存在");
        }else {
            if (rider1.getRiderstatus()){
                if (login_name.equals(rider1.getLogin_name())&&s.equals(rider1.getLogin_pwd())){
                    return RespBean.success("登陆成功");
                } else {
                    return RespBean.error("用户名或密码错误");
                }
            }else{
                return RespBean.error("用户还没有审核");
            }
        }
//        return "rider/index";
    }

    @GetMapping("/register")
    public String registerindex(){
        return "rider/register";
    }
    @PostMapping("/register")
    @ResponseBody
    public synchronized RespBean register(Rider rider){
        System.out.println(rider);
        Rider rider1 = service.selectAllByloginName(rider.getLogin_name());
        if (rider1==null){
            System.out.println("123");
            int insert = service.insert(rider);
            if (insert>0){
                System.out.println("注册成功");
                return RespBean.success("注册成功");
            }else {
                System.out.println("注册失败");
                return RespBean.error("注册失败");
            }
        }else {
            return RespBean.error("用户名已存在");
        }
    }


}
